package cn.swing.ushare.user.webapi.controller;

import cn.hutool.core.util.StrUtil;
import cn.swing.ushare.common.web.JsonResult;


import cn.swing.ushare.pojo.dto.UserLimitSimpleDTO;
import cn.swing.ushare.pojo.dto.UserLoginDTO;
import cn.swing.ushare.pojo.dto.UserUpdateRoleDTO;
import cn.swing.ushare.pojo.dto.*;
import cn.swing.ushare.pojo.vo.*;
import cn.swing.ushare.user.service.IUserService;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/users", produces = "application/json;charset=utf-8")
public class UserController {

    @Autowired
    private IUserService service;


    @GetMapping("/list/changeEnable")
    @PreAuthorize("hasAnyAuthority('用户启用禁用')")
    public JsonResult<Void> updateUserEnableById(Integer id) {
        service.updateEnabledById(id);
        return JsonResult.ok();
    }


    @GetMapping("/list/limitedUser")
//    @PreAuthorize("hasAnyAuthority('使用搜索')")
    public List<UserListVO> search(String nickname, Integer page) {
        String newNickname = nickname.replaceAll(" +", "");
        System.out.println("nickname = " + nickname + ", page = " + page);
        UserLimitSimpleDTO userLimitSimpleDTO = new UserLimitSimpleDTO();
        userLimitSimpleDTO.setNickname(newNickname);
        userLimitSimpleDTO.setPage((page - 1) * 10);
        System.out.println("userLimitSimpleDTO = " + userLimitSimpleDTO);
        return service.userList(userLimitSimpleDTO);
    }

    @GetMapping("/countUser")
    public Integer count(String nickname) {
        String newNickname="";
        if(nickname != null){
            newNickname = nickname.replaceAll(" +", "");
        }
        return service.countUser(newNickname);

    }

    /****************************************ljk改**********************************/
    @GetMapping("/personal")
    public JsonResult<PersonalSimpleVO> getSimplePersonal() {

        return JsonResult.ok(service.getPersonalSimple());
    }

    @PostMapping("/updatePass")
    public JsonResult<Integer> updatePass(@RequestBody UserChangePasswordDTO userChangePasswordDTO) {
        System.out.println("userChangePasswordDTO = " + userChangePasswordDTO);
        return JsonResult.ok(service.updatePassByUsername(userChangePasswordDTO));

    }

    /****************************************ljk改**********************************/

    @PostMapping("/list/changeRole")
    @PreAuthorize("hasAnyAuthority('角色分配')")
    public JsonResult<Void> changeRole(@Validated UserUpdateRoleDTO userUpdateRoleDTO) {
        service.updateUserRole(userUpdateRoleDTO);
        return JsonResult.ok();
    }


    @GetMapping("/userDetails")
    public JsonResult<UserCenterDetailsVO> userDetailsById(Integer id) {

        UserCenterDetailsVO userCenterDetailsVO = service.getUserCenterDetailsById(id);
        return JsonResult.ok(userCenterDetailsVO);
    }

    @GetMapping("/userHeader")
    public JsonResult<UserCenterHeaderVO> getUserCenterHeaderById(Integer id) {
        UserCenterHeaderVO userCenterHeaderVO = service.getUserCenterHeaderById(id);
        return JsonResult.ok(userCenterHeaderVO);
    }

    @GetMapping("/userCenterCategory")
    public JsonResult<List<CategorySimpleVO>> getUserCenterCategory(Integer id) {
        List<CategorySimpleVO> list = service.getUserCenterCategory(id);
        return JsonResult.ok(list);
    }

    @GetMapping("/getAllCategory")
    public JsonResult<List<CategorySimpleVO>> getAllCategory(Integer id) {
        List<CategorySimpleVO> list = service.getAllCategory(id);
        return JsonResult.ok(list);
    }

    @GetMapping("/roleManagement")
    public JsonResult<List<RoleListVO>> roleList() {
        List<RoleListVO> list = service.roleList();
        return JsonResult.ok(list);
    }


    @GetMapping("/currentHeadImage")
    public JsonResult<String> getCurrentAvatar(Integer id) {
        String avatarUrl = service.getCurrentAvatar(id);
        return JsonResult.ok(avatarUrl);
    }

    @GetMapping("/getArticleDetailsUserInfo")
    public JsonResult<UserCenterHeaderVO> getArticleDetailsUserInfo(Integer id) {
        UserCenterHeaderVO userCenterHeaderVO = service.getArticleDetailsUserInfoById(id);
        return JsonResult.ok(userCenterHeaderVO);
    }

    @GetMapping("/userFollowsSimple")
    public JsonResult<List<UserSimpleVO>> getAllFollows(Integer id) {
        List<UserSimpleVO> list = service.getAllFollows(id);
        return JsonResult.ok(list);
    }

    @GetMapping("/userFansSimple")
    public JsonResult<List<UserSimpleVO>> getAllFans(Integer id) {
        List<UserSimpleVO> list = service.getAllFans(id);
        return JsonResult.ok(list);
    }

    @PostMapping("/insertRole")
    @PreAuthorize("hasAnyAuthority('角色新增')")
    public JsonResult<Void> insertRole(@Param("roleName") String roleName) {
        service.insertRole(roleName);
        return JsonResult.ok();
    }

    @GetMapping("/deleteRoleById")
    @PreAuthorize("hasAnyAuthority('角色删除')")
    public JsonResult<Void> deleteRoleById(Integer id) {
        service.deleteRoleById(id);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    public boolean login(@RequestBody UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return false;
        }
        return service.login(userLoginDTO);
    }


    @RequestMapping("/updateUserCenterDetails")
    public JsonResult<Void> updateUserCenterDetails(@Validated @RequestBody UserCenterDetailsDTO userCenterDetailsDTO) {
        service.updateUserCenterDetails(userCenterDetailsDTO);
        return JsonResult.ok();
    }

    @PostMapping("/deleteUserCenterCategoryById")
    public JsonResult<Void> deleteUserCenterCategoryById(Integer userId) {
        service.deleteUserCenterCategoryById(userId);
        return JsonResult.ok();
    }


    @RequestMapping("/insertUserCenterCategories")
    public JsonResult<Void> insertUserCenterCategories(@RequestBody List<UserCenterCategoryDTO> userCenterCategories) {
        service.insertUserCenterCategories(userCenterCategories);
        return JsonResult.ok();
    }

    @GetMapping("/getPermissions")
    public JsonResult<List<PermissionListVO>> getPermissions() {
        List<PermissionListVO> list = service.getPermissions();
        return JsonResult.ok(list);
    }


    @PostMapping("/getPermissionById")
    public JsonResult<List<SelectPermissionVO>> getPermissionById(@Param("roleId") Integer roleId) {
        List<SelectPermissionVO> list = service.getPermissionById(roleId);
        return JsonResult.ok(list);
    }


    @PostMapping("/changeRoleOperationById")
    public JsonResult<String> changeRoleOperation(@RequestBody List<ChangeRoleOperationDTO> changeRoleOperations) {
        service.changeRoleOperation(changeRoleOperations);
        return JsonResult.ok("插入成功");
    }

    @RequestMapping("/userIsFollow")
    public JsonResult<Boolean> isFollow(Integer id,Integer userId){

        return JsonResult.ok(service.isFollow(id, userId));
    }

    @PostMapping("/userFollowed")
    public JsonResult<Integer> insertFollow(@Param("id") Integer id, @Param("userId") Integer userId){
        return JsonResult.ok(service.insertFollow(id, userId));
    }

    @PostMapping("/userUnfollow")
    public JsonResult<Integer> deleteFollow(@Param("id") Integer id, @Param("userId") Integer userId){
        return JsonResult.ok(service.deleteFollow(id, userId));
    }


    @PostMapping("/userRegister")
    public JsonResult<String> insertRegisterUser(@RequestBody UserRegisterSimpleDTO userRegisterSimpleDTO) {
        String message = service.insertRegister(userRegisterSimpleDTO);
        System.out.println("message???????????????" + message);
        System.out.println("userRegisterSimpleDTO = " + userRegisterSimpleDTO);
        if (message.equals("用户注册成功")) {
            return JsonResult.ok("用户插入成功");
        } else {
            return JsonResult.ok("用户已经存在");
        }
    }

    //修改用户头像
    @PostMapping("/updateAvatar")
    public JsonResult<Void> updateUserAvatar(Integer userId, String imgUrl){
        System.out.println("userId = " + userId);
        System.out.println("avatar = " + imgUrl);
        service.updateUserAvatar(userId, imgUrl);
        return JsonResult.ok();
    }


    @PostMapping("/selectUsernameByNickname")
    public JsonResult<String> selectUsernameByNickname(String nickname){
        System.out.println("nickname = " + nickname);
        return JsonResult.ok(service.selectUsernameByNickname(nickname));
    }

    @GetMapping("/currentLogin")
    public JsonResult<Integer> getLoginUserId(){
        Integer userId = service.getLoginUserId();
        return JsonResult.ok(userId);
    }

    @PostMapping("/addUserFollowers")
    public JsonResult<Integer> addUserFollowers(@Param("userId") Integer userId){
        Integer result = service.addUserFollowers(userId);
        return JsonResult.ok(result);
    }
    @PostMapping("/subUserFollowers")
    public JsonResult<Integer> subUserFollowers(@Param("userId") Integer userId){
        Integer result = service.subUserFollowers(userId);
        return JsonResult.ok(result);
    }


    @PostMapping("/addUserFans")
    public JsonResult<Integer> addUserFans(@Param("userId") Integer userId){
        Integer result = service.addUserFans(userId);
        return JsonResult.ok(result);
    }
    @PostMapping("/subUserFans")
    public JsonResult<Integer> subUserFans(@Param("userId") Integer userId){
        Integer result = service.subUserFans(userId);
        return JsonResult.ok(result);
    }

    @GetMapping("/PerfectInformation")
    public JsonResult<String> PerfectInformation(Integer id) {
        String str = service.PerfectInformation(id);
        return JsonResult.ok(str);
    }

}
